package com.jeeplus.modules.project.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.ProjectApply;
import com.jeeplus.modules.project.entity.ProjectMidteamExam;
import com.jeeplus.modules.project.entity.VBaseProjectMidteamEaxm;
import com.jeeplus.modules.project.service.BaseProjectInfoService;
import com.jeeplus.modules.project.service.ProjectMidteamExamService;
import com.jeeplus.modules.project.service.VBaseProjectMidteamEaxmService;

/**
 * 中期检查Controller
 * @author jacky
 * @version 2018-01-10
 */
@Controller
@RequestMapping(value = "${adminPath}/project/projectMidteamExam")
public class ProjectMidteamExamController extends BaseController {
	@Autowired
	private ProjectMidteamExamService projectMidteamExamService;
	@Autowired
	private VBaseProjectMidteamEaxmService projectMidteamEaxmService;
	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	@Autowired
	private VBaseProjectMidteamEaxmService baseProjectMidteamEaxmService;
	@ModelAttribute
	public ProjectMidteamExam get(@RequestParam(required=false) String id) {
		ProjectMidteamExam entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectMidteamExamService.get(id);
		}
		if (entity == null){
			entity = new ProjectMidteamExam();
		}
		return entity;
	}
	@ModelAttribute(value="vBaseProjectMidteamEaxm")
	public VBaseProjectMidteamEaxm add(@RequestParam(required=false) String examId) {
		VBaseProjectMidteamEaxm entity = null;
		if (StringUtils.isNotBlank(examId)){
			VBaseProjectMidteamEaxm baseProjectMidteamEaxm = new VBaseProjectMidteamEaxm();
			entity = baseProjectMidteamEaxmService.get(baseProjectMidteamEaxm);
		}
		if (entity == null){
			entity = new VBaseProjectMidteamEaxm();
		}
		return entity;
	}
	@RequiresPermissions("project:projectMidteamExam:index")
	@RequestMapping(value = "index")
	public String index(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		baseProjectInfo.setStatus(5);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/midterm/basePtInfoMidList";
	}
	
	@RequiresPermissions("project:projectMidteamExam:index")
	@RequestMapping(value = "select")
	public String select(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		//baseProjectInfo.setStatus(5);
		baseProjectInfo.setFlag(1);
		List<Integer> statusArray = new ArrayList<Integer>();
		statusArray.add(2);
		statusArray.add(3);
		statusArray.add(4);
		baseProjectInfo.setStatusArray(statusArray);
		Page<BaseProjectInfo> page = baseProjectInfoService.findPage(new Page<BaseProjectInfo>(request, response), baseProjectInfo); 
		model.addAttribute("page", page);
		return "modules/project/midterm/basePtInfoMidSelect";
	}
	
	@RequiresPermissions("project:projectMidteamExam:index")
	@RequestMapping(value = "track")
	public String track(VBaseProjectMidteamEaxm baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
//		baseProjectInfo.setStatus(5);
		Page<VBaseProjectMidteamEaxm> page2 = new Page<VBaseProjectMidteamEaxm>(request, response);
		page2.setOrderBy("bptinfo_id asc");
		Page<VBaseProjectMidteamEaxm> page = baseProjectMidteamEaxmService.findPage(page2, baseProjectInfo); 
		model.addAttribute("courseName", baseProjectInfo.getCourseName());
		model.addAttribute("hostName", baseProjectInfo.getHostName());
		model.addAttribute("projectNo", baseProjectInfo.getProjectNo());
		model.addAttribute("totalProcess", baseProjectInfo.getTotalProcess());
		model.addAttribute("status", baseProjectInfo.getStatus());
		model.addAttribute("vBaseProjectMidteamEaxm",baseProjectInfo);
		model.addAttribute("page", page);
		return "modules/project/midterm/projectMidteamExamInfoList";
	}
	
	/**
	 * 中期检查列表页面
	 */
	@RequiresPermissions("project:projectMidteamExam:list")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectMidteamExam projectMidteamExam, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectMidteamExam> page = projectMidteamExamService.findPage(new Page<ProjectMidteamExam>(request, response), projectMidteamExam); 
		model.addAttribute("page", page);
		return "modules/project/midterm/examList";
	}
	
	/**
	 * 审批情况
	 * @param projectMidteamExam
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectMidteamExam:list")
	@RequestMapping(value = "approvList")
	public String approvList(ProjectMidteamExam projectMidteamExam, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectMidteamExam> page = projectMidteamExamService.findPage(new Page<ProjectMidteamExam>(request, response), projectMidteamExam); 
		model.addAttribute("page", page);
		return "modules/project/midterm/projectMidteamExamList";
	}

	/**
	 * 查看，增加，编辑中期检查表单页面
	 */
	@RequiresPermissions(value={"project:projectMidteamExam:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(ProjectMidteamExam projectMidteamExam, Model model) {
		if(projectMidteamExam.getBptInfo() != null){
			if(!StringUtils.isEmpty(projectMidteamExam.getBptInfo().getId())){
				BaseProjectInfo bptInfo = baseProjectInfoService.get(projectMidteamExam.getBptInfo().getId());
				projectMidteamExam.setBptInfo(bptInfo);
			}
		}
		String view = "examForm";
		// 查看审批申请单
		if(StringUtils.isNotBlank(projectMidteamExam.getId())){
			// 环节编号
			// 查看工单
			if(projectMidteamExam.getAct().isFinishTask()){
				view = "examViewForm";
			}else{
				view = "examApproveForm";
			}
		}
		model.addAttribute("projectMidteamExam", projectMidteamExam);
		return "modules/project/midterm/"+view;
	}

	/**
	 * 保存中期检查
	 */
	@RequiresPermissions(value={"project:projectMidteamExam:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(ProjectMidteamExam projectMidteamExam, Model model, RedirectAttributes redirectAttributes) throws Exception{
		projectMidteamExamService.save(projectMidteamExam);//保存
		addMessage(redirectAttributes, "保存中期检查成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectMidteamExam/track?bptInfo.id="+projectMidteamExam.getBptInfo().getId();
	}
	
	/**
	 * 删除中期检查
	 */
	@RequiresPermissions("project:projectMidteamExam:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectMidteamExam projectMidteamExam, RedirectAttributes redirectAttributes) {
		projectMidteamExamService.delete(projectMidteamExam);
		addMessage(redirectAttributes, "删除中期检查成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectMidteamExam/?repage";
	}
	
	/**
	 * 批量删除中期检查
	 */
	@RequiresPermissions("project:projectMidteamExam:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			projectMidteamExamService.delete(projectMidteamExamService.get(id));
		}
		addMessage(redirectAttributes, "删除中期检查成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectMidteamExam/?repage";
	}
	/**
	 * 工单执行（审核-完成任务）
	 * @param testAudit
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:projectMidteamExam:edit")
	@RequestMapping(value = "saveAprov")
	public String saveAprov(ProjectMidteamExam projectMidteamExam, Model model) {
		if (StringUtils.isBlank(projectMidteamExam.getAct().getFlag())
				|| StringUtils.isBlank(projectMidteamExam.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return form(projectMidteamExam, model);
		}
		projectMidteamExamService.approveSave(projectMidteamExam);
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	
	@RequestMapping(value = "InfoList")
	public String InfoList(VBaseProjectMidteamEaxm projectMidteamExam,HttpServletRequest request,HttpServletResponse response,Model model) {
		Page<VBaseProjectMidteamEaxm> findPage = projectMidteamEaxmService.findPage(new Page<VBaseProjectMidteamEaxm>(request, response), projectMidteamExam);
		model.addAttribute("page", findPage);
		return  "modules/project/midterm/projectMidteamExamInfoList";
	}
	
}